package com.dg.river.module.main.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.dg.river.R;
import com.dg.river.contant.CommonCode;
import com.dg.river.core.inject.Presenter;
import com.dg.river.core.util.L;
import com.dg.river.core.util.ToastUtils;
import com.dg.river.core.util.Utils;
import com.dg.river.databinding.ActivityMainBinding;
import com.dg.river.module.base.BaseActivity;
import com.dg.river.module.common.bean.EbMessage;
import com.dg.river.module.main.contract.DemoContract;
import com.dg.river.module.main.fragment.HomeFragment;
import com.dg.river.module.main.presenter.DemoPresenter;
import com.dg.river.netty.auditdev.AuditDevClient;
import com.dg.river.netty.axle.msg2dev.MessageConst;
import com.dg.river.netty.axledev.NotifyConst;
import com.gyf.immersionbar.ImmersionBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * @作者：HuYong
 * @时间：2023/4/12 16:19
 * @描述：首页
 * @xml：activity_main.xml
 * @修改：
 */
public class MainActivity extends BaseActivity implements DemoContract.IDemoView {

    @SuppressLint("HandlerLeak")
    private Handler axleDevMsgHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            L.d("Hy","--"+msg.what);
            switch (msg.what) {
                case NotifyConst.MT_NETSTAT:
                    //
                    L.d("Hy","链接成功");

                    break;
                case MessageConst.MTYPE_RETURN:
                    //onAxleReturn(msg);
                    break;
                default:
                    break;
            }
        }
    };

    private ActivityMainBinding binding;
    private Fragment currentFragment = new Fragment();
    private HomeFragment homeFragment;

    private int tab = 0;

    @Presenter
    DemoPresenter demoPresenter;

    // 车轴检测设备客户端
    private AuditDevClient axleDevClient;

    public static void start(Context mContext) {
        Intent intent = new Intent(mContext, MainActivity.class);
        mContext.startActivity(intent);
    }

    public static void startTop(Context mContext) {
        Intent intent = new Intent(mContext, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }

    @NonNull
    @Override
    public View initLayout() {
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    protected void initGetIntent() {
        super.initGetIntent();
        tab = getIntent().getIntExtra("tab", 0);
    }

    @Override
    public void initView() {

    }

    @Override
    public boolean viewAndData() {
        return true;
    }

    @Override
    public void initData() {
        ImmersionBar.with(this).statusBarDarkFont(false).init();
        // 创建EventBus
        EventBus.getDefault().register(this);
        // 初始化Fragment
        clickFragment(tab);
        // 调用接口
        demoPresenter.demoFun1("1111");
        initNettyClient();
    }

    @Override
    public void initListener() {
        binding.llHome.setOnClickListener(v -> {
            clickFragment(0);
        });
        binding.llNews.setOnClickListener(v -> {
            clickFragment(1);
        });
        binding.llChannel.setOnClickListener(v -> {
            clickFragment(2);
        });
        binding.llMessage.setOnClickListener(v -> {
            clickFragment(3);
        });
        binding.llMine.setOnClickListener(v -> {
            clickFragment(4);
        });
    }

    public void initNettyClient(){
        axleDevClient = new AuditDevClient(axleDevMsgHandler);
        axleDevClient.start();
    }

    public void clickFragment(int position) {
        switch (position) {
            case 0:
                ImmersionBar.with(this).statusBarDarkFont(false).init();
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                }
                if (showFragment(homeFragment)) {
                    showSelect(position);
                }
                break;
            case 1:
                ToastUtils.getInstance().toast("这是第二个");
                break;
            case 2:
                ToastUtils.getInstance().toast("这是第三个");
                break;
            case 3:
                ToastUtils.getInstance().toast("这是第四个");
                break;
            case 4:
                ToastUtils.getInstance().toast("这是第五个");
                break;
        }
    }

    private void showSelect(int position) {
        switch (position) {
            case 0:
                binding.llHome.setSelected(true);
                binding.llNews.setSelected(false);
                binding.llChannel.setSelected(false);
                binding.llMessage.setSelected(false);
                binding.llMine.setSelected(false);
                break;
            case 1:
                binding.llHome.setSelected(false);
                binding.llNews.setSelected(true);
                binding.llChannel.setSelected(false);
                binding.llMessage.setSelected(false);
                binding.llMine.setSelected(false);
                break;
            case 2:
                binding.llHome.setSelected(false);
                binding.llNews.setSelected(false);
                binding.llChannel.setSelected(true);
                binding.llMessage.setSelected(false);
                binding.llMine.setSelected(false);
                break;
            case 3:
                binding.llHome.setSelected(false);
                binding.llNews.setSelected(false);
                binding.llChannel.setSelected(false);
                binding.llMessage.setSelected(true);
                binding.llMine.setSelected(false);
                break;
            case 4:
                binding.llHome.setSelected(false);
                binding.llNews.setSelected(false);
                binding.llChannel.setSelected(false);
                binding.llMessage.setSelected(false);
                binding.llMine.setSelected(true);
                break;
        }
    }

    private boolean showFragment(Fragment fragment) {
        FragmentManager manager = getSupportFragmentManager();
        if (currentFragment != fragment) {
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.hide(currentFragment);
            currentFragment = fragment;
            if (!fragment.isAdded()) {
                transaction.add(R.id.fl_main, fragment).show(fragment).commitAllowingStateLoss();
            } else {
                transaction.show(fragment).commitAllowingStateLoss();
            }
            return true;
        } else {
            return false;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EbMessage message) {
        if (CommonCode.EventBus.REFRESH_MAIN_UNREAD_COUNT.equals(message.getMsg())) {

        }
    }

    @Override
    public void showErrorMessage(@Nullable String message) {

    }

    @Override
    public void demoFun1Success(@NonNull String demo) {
        ToastUtils.getInstance().toast(demo);
    }

    @Override
    public void demoFun1Failure(@NonNull String demo) {

    }

    @Override
    public void demoFun2Success(@NonNull String demo) {

    }

    @Override
    public void demoFun2Failure(@NonNull String demo) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Utils.clickToExit(mContext);
        }
        return false;
    }
}
